<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Expression (computer science)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Expression_(computer_science)"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Expression_computer_science rootpage-Expression_computer_science skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Expression (computer science)</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>In <a href="Computer_science" title="Computer science">computer science</a>, an <b>expression</b> is a <a href="Syntax_(programming_languages)" title="Syntax (programming languages)">syntactic</a> entity in a <a href="Programming_language" title="Programming language">programming language</a> that may be evaluated to determine its <a href="Value_(computer_science)" title="Value (computer science)">value</a>.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> It is a combination of one or more <a href="Constant_(programming)" class="mw-redirect" title="Constant (programming)">constants</a>, <a href="Variable_(programming)" class="mw-redirect" title="Variable (programming)">variables</a>, <a href="Function_(programming)" class="mw-redirect" title="Function (programming)">functions</a>, and <a href="Operator_(programming)" class="mw-redirect" title="Operator (programming)">operators</a> that the programming language interprets (according to its particular <a href="Order_of_operations" title="Order of operations">rules of precedence</a> and of <a href="Associative_property" title="Associative property">association</a>) and computes to produce ("to return", in a <a href="State_(computer_science)" title="State (computer science)">stateful</a> environment) another value. This process, for <a href="Mathematical_expression" class="mw-redirect" title="Mathematical expression">mathematical expressions</a>, is called <i>evaluation</i>.
In simple settings, the <a href="Return_type" title="Return type">resulting value</a> is usually one of various <a href="Primitive_data_type" title="Primitive data type">primitive types</a>, such as <a href="String_(computer_science)" title="String (computer science)">string</a>, <a href="Boolean_expression" title="Boolean expression">boolean</a>, or numerical (such as <a href="Integer_(computer_science)" title="Integer (computer science)">integer</a>, <a href="Floating-point_number" class="mw-redirect" title="Floating-point number">floating-point</a>, or <a href="Complex_data_type" title="Complex data type">complex</a>).
</p><p>Expressions are often contrasted with <a href="Statement_(computer_science)" title="Statement (computer science)">statements</a>—<a href="Syntactic" class="mw-redirect" title="Syntactic">syntactic</a> entities that have no value (an instruction).
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Examples">Examples</h2></div>
<p><code>2 + 3</code> is both an <a href="Arithmetic_expression" class="mw-redirect" title="Arithmetic expression">arithmetic</a> and programming expression, which evaluates to <code>5</code>. A variable is an expression because it denotes a value in <a href="Memory_(computer)" class="mw-redirect" title="Memory (computer)">memory</a>, so <code>y + 6</code> is also an expression. An example of a <a href="Relational_operator" title="Relational operator">relational expression</a> is <code>4 ≠ 4</code>, which evaluates to <code>false</code>.<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Void_as_a_result_type">Void as a result type</h2></div>
<p>In <a href="C_(programming_language)" title="C (programming language)">C</a> and most C-derived languages, a call to a function with a <a href="Void_type" title="Void type">void</a> return type is a valid expression, of type void.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>
Values of type void cannot be used, so the value of such an expression is always thrown away.
</p>
<div class="mw-heading mw-heading2"><h2 id="Side_effects_and_elimination">Side effects and elimination</h2></div>
<p>In many programming languages, a function, and hence an expression containing a function, may have <a href="Side_effect_(computer_science)" title="Side effect (computer science)">side effects</a>. An expression with side effects does not normally have the property of <a href="Referential_transparency" title="Referential transparency">referential transparency</a>. In many languages (e.g. <a href="C%2B%2B" title="C++">C++</a>), expressions may be ended with a semicolon (<code>;</code>) to turn the expression into an expression <a href="Statement_(programming)" class="mw-redirect" title="Statement (programming)">statement</a>. This asks the implementation to evaluate the expression for its side-effects only and to disregard the result of the expression (e.g. <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">x+1;</code>) unless it is a part of an expression statement that induces side-effects (e.g. <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">y=x+1;</code> or <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">func1(func2());</code>).
</p>
<div class="mw-heading mw-heading3"><h3 id="Caveats">Caveats</h3></div>
<p>The formal notion of a side effect is a change to the abstract state of the running program.
</p><p>Another class of side effects are changes to the concrete state of the computational system, such as loading data into <a href="Cache_memory" class="mw-redirect" title="Cache memory">cache memories</a>. Languages that are often described as "side effect–free" will generally still have concrete side effects that can be exploited, for example, in <a href="Side-channel_attack" title="Side-channel attack">side-channel attacks</a>.
</p><p>Furthermore, the elapsed time evaluating an expression (even one with no other apparent side effects), is sometimes essential to the correct operation of a system, as behaviour in time is easily visible from outside the evaluation environment by other parts of the system with which it interacts, and might even be regarded as the <i>primary</i> effect such as when performing <a href="Benchmark_(computing)" title="Benchmark (computing)">benchmark testing</a>.
</p><p>It depends on the particular programming language specification whether an expression with no abstract side effects can <a href="Program_optimization" title="Program optimization">legally be eliminated</a> from the execution path by the processing environment in which the expression is evaluated.
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Evaluation_strategy" title="Evaluation strategy">Evaluation strategy</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text"><a href="John_C._Mitchell" title="John C. Mitchell">Mitchell, J.</a> (2002). Concepts in Programming Languages. Cambridge: Cambridge University Press, <i>3.4.1 Statements and Expressions</i>, p. 26</span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Expressions">Javascript expressions, Mozilla</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20120309174344/https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Expressions">Archived</a> 2012-03-09 at the <a href="Wayback_Machine" title="Wayback Machine">Wayback Machine</a> Accessed July 6, 2009</span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://www.cs.drexel.edu/~rweaver/COURSES/ISTC-2/TOPICS/expr.html">Programming in C</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20150109162605/https://www.cs.drexel.edu/~rweaver/COURSES/ISTC-2/TOPICS/expr.html">Archived</a> 2015-01-09 at the <a href="Wayback_Machine" title="Wayback Machine">Wayback Machine</a> Accessed July 6, 2009</span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text">
<a rel="nofollow" class="external text" href="http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf">ISO/IEC 9899:1999</a> section 6.3.2.2, accessed August 31, 2009</span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><style data-mw-deduplicate="TemplateStyles:r1041539562">
/* start https://en.wikipedia.org/ */
.mw-parser-output .citation{word-wrap:break-word}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}
/* end https://en.wikipedia.org/ */
</style><span class="citation foldoc">This article is based on material taken from <a rel="nofollow" class="external text" href="https://foldoc.org/Expression">Expression</a> at the <i><a href="Free_On-line_Dictionary_of_Computing" title="Free On-line Dictionary of Computing">Free On-line Dictionary of Computing</a></i> prior to 1 November 2008 and incorporated under the "relicensing" terms of the <a href="GNU_Free_Documentation_License" title="GNU Free Documentation License">GFDL</a>, version 1.3 or later.</span></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-02-11" href="https://en.wikipedia.org/wiki/?title=Expression_(computer_science)&oldid=1275164713">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>